#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using WaveMaster.Holidays.Club.Service;
using System.Configuration;
using System.Collections;
using ThoughtWorks.QRCode.Codec;
using System.IO;
using WaveMaster.Holidays.Club.Domain;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class PrintBoardingPassForm : Form
    {
        CheckInService m_BookingService;
        
        public PrintBoardingPassForm(long id, User m_currentUser, bool oneway)
        {
            InitializeComponent();
            this.Text = "Print Boarding Pass";

            m_BookingService = new CheckInService();// (CheckInService)ServiceFactory.Instance().GetService(ServiceFactory.BOOKING_SERVICE);
            DataSet result = m_BookingService.ReportItineraryBookingPassengerByID(id);
            result.Tables[0].Columns.Add("barcodeImage", typeof(byte[]));
            ArrayList l = new ArrayList();
            int i = result.Tables[0].Rows[0].ItemArray.Length;
            int j = 0;
            foreach (object o in result.Tables[0].Rows[0].ItemArray)
            {
                if (j == (i - 1))
                {
                    l.Add(getBarcode2DImage(l[23].ToString()));// index 23 is boarding pass code.
                }
                else
                    l.Add(o);
                j++;
            }
            result.Tables[0].Rows[0].ItemArray = l.ToArray();
            result.WriteXml(ConfigurationManager.AppSettings["ReportDataTemp"] + "ReportBoardingPass" + m_currentUser.UserID + ".xml", XmlWriteMode.WriteSchema);

            if (oneway)
            {
                //result.WriteXml(@"D:\My Projects\WaveMasterBoardingManagementSystem\ReportBoardingPass.xml", XmlWriteMode.WriteSchema);
                htmlBox1.Url = ConfigurationManager.AppSettings["URLReport"] + "ReportViewer.aspx?ReportDataSource=ReportBoardingPass" + m_currentUser.UserID + ".xml&ReportFile=ReportBoardingPassOneWay.rpt";
                htmlBox1.Update();
            }
            else
            {
                //result.WriteXml(@"D:\My Projects\WaveMasterBoardingManagementSystem\ReportBoardingPass.xml", XmlWriteMode.WriteSchema);
                htmlBox1.Url = ConfigurationManager.AppSettings["URLReport"] + "ReportViewer.aspx?ReportDataSource=ReportBoardingPass" + m_currentUser.UserID + ".xml&ReportFile=ReportBoardingPassTwoWay.rpt";
                htmlBox1.Update();
            }

           // string reportID = ServiceFactory.GenerateRandomID().ToString();
           // Session["ReportBoardingPassData" + reportID] = result;
           // Session["ReportBoardingPassFile" + reportID] = "ReportBoardingPass.rpt";
           // string param1 = "ReportDataSource=ReportBoardingPassData" + reportID;
           // string param2 = "ReportFile=ReportBoardingPassFile" + reportID;
           // htmlBox1.Url = ConfigurationManager.AppSettings["URLReport"] + "ReportViewer.aspx?" + param1 + "&" + param2;
           //// htmlBox1.Url = ConfigurationManager.AppSettings["URLReport"] + "ReportPrintDirectly.aspx?" + param1 + "&" + param2;

           // //htmlBox1.Url = "http://" + this.Context.HttpContext.Request.ServerVariables["SERVER_NAME"] + ":" + this.Context.HttpContext.Request.ServerVariables["SERVER_PORT"] + @"/Report/" + "ReportViewer.aspx?" + param1 + "&" + param2;
           // htmlBox1.Update();
        }

        public byte[] getBarcode2DImage(string text)
        {
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            qrCodeEncoder.QRCodeScale = 6;
            qrCodeEncoder.QRCodeVersion = 3;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            Image image = qrCodeEncoder.Encode(text);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
    }
}